package com.zabra.zl.ui.personal_center;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.AccountBean;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.addbank.AddBankActivity;
import com.zabra.zl.ui.login.LoginActivity;
import com.zabra.zl.ui.mybank.MyBankActivity;
import com.zabra.zl.ui.nullpage.NullPersonActivity;
import com.zabra.zl.ui.personal_center.contract.PersonalContract;
import com.zabra.zl.ui.personal_center.presenter.PersonalPresenter;
import com.zabra.zl.ui.verifyperson.VerifyPersonActivity;
import com.zabra.zl.ui.verifyphone.VerifyPhoneActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PersonalActivity extends BaseMvpActivity<PersonalPresenter> implements PersonalContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_personverify)
    RelativeLayout rlPersonverify;
    @BindView(R.id.rl_bank)
    RelativeLayout rlBank;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @BindView(R.id.rl_loginpwd)
    RelativeLayout rlLoginpwd;
    @BindView(R.id.rl_dealpwd)
    RelativeLayout rlDealpwd;
    @BindView(R.id.tv_logout)
    TextView tvLogout;
    @BindView(R.id.tv_verify)
    TextView tvVerify;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.tv_loginpwd)
    TextView tvLoginpwd;
    @BindView(R.id.tv_dealpwd)
    TextView tvDealpwd;
    private String strTel, strUserName, strIDcard;
    private Dialog dialog;
    private int statePerson, stateBank, stateDealpwd;
    private Bundle bundle = new Bundle();
    private EventMessage message;

    @Override
    public int getLayoutId() {
        return R.layout.activity_personal;
    }

    @Override
    public void initView() {
        tvTitle.setText("个人中心");

        keyList.add("debtorId");
        valueList.add(strUserid);
        mPresenter = new PersonalPresenter();
        mPresenter.attachView(this);
        mPresenter.getUserData(MaptoJsonUtils.Simple(keyList, valueList));
    }


    @OnClick({R.id.back, R.id.rl_personverify, R.id.rl_bank, R.id.rl_phone, R.id.rl_loginpwd, R.id.rl_dealpwd, R.id.tv_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.rl_personverify:
                //实名认证
                bundle.putInt("isbundle", statePerson);
                message = new EventMessage(1, "Person");
                EventBus.getDefault().postSticky(message);
                toClass1(this, VerifyPersonActivity.class, bundle);
                break;
            case R.id.rl_bank:
                //银行卡，没有实名认证需先实名
                if (statePerson == 1) {
                    if (stateBank == 1) {
                        toClass(this, MyBankActivity.class);
                    } else {
                        bundle.putString("dealpwd", "no");
                        toClass1(this, AddBankActivity.class, bundle);
                        message = new EventMessage(1, "Person");
                        EventBus.getDefault().postSticky(message);
                    }
                } else {
                    toClass1(this, NullPersonActivity.class, bundle);
                }
                break;
            case R.id.rl_phone:
                //手机号
                bundle.putInt("type", Config.CHANGTEL);
                toClass(this, VerifyPhoneActivity.class, bundle);
                break;
            case R.id.rl_loginpwd:
                //登录密码
                bundle.putInt("type", Config.LOGINPWD);
                toClass(this, VerifyPhoneActivity.class, bundle);
                break;
            case R.id.rl_dealpwd:
                //交易密码
                message = new EventMessage(1, "Person");
                EventBus.getDefault().postSticky(message);
                if (stateBank == 1) {
                    //绑定银行卡
                    bundle.putInt("type", Config.DEALPWD);
                    toClass(this, VerifyPhoneActivity.class, bundle);
                } else {
                    //没有绑定银行卡先绑定
                    bundle.putString("dealpwd", "no");
                    toClass1(this, AddBankActivity.class, bundle);
                }

                break;
            case R.id.tv_logout:
                //退出登录
                loginOut();
                break;
        }
    }

    private void loginOut() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_logout, null);
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                keyList.add("phone");
                valueList.add(strTel);
                mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));

                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }


    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            SharedPreferencesUtil.getInstance(this).putSP("token", "");
            SharedPreferencesUtil.getInstance(this).putSP("debtorid", "");
            toClass_Empty(this, LoginActivity.class);
            finish();
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(AccountBean bean, String token) {
        //1认证  0未认证
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            statePerson = bean.getAttestation();
            stateBank = bean.getIsBindBankCard();
            stateDealpwd = bean.getHavaTranPwd();
            strTel = bean.getTel();
            tvTel.setText(strTel);
            if (statePerson == 1) {
                strUserName = bean.getUserName();
                strIDcard = bean.getSfzh();
                //保存姓名和身份证
                SharedPreferencesUtil.getInstance(this).putSP("username", strUserName);
                SharedPreferencesUtil.getInstance(this).putSP("idcard", strIDcard);
                tvVerify.setText("已认证");
                tvVerify.setTextColor(Color.parseColor("#999999"));
            } else {
                tvVerify.setText("未认证");
                tvVerify.setTextColor(Color.parseColor("#ff3025"));
            }
            if (stateBank == 1) {
                tvBank.setText("已绑定");
                tvBank.setTextColor(Color.parseColor("#999999"));
            } else {
                tvBank.setText("未绑定");
                tvBank.setTextColor(Color.parseColor("#ff3025"));
            }
            if (stateDealpwd == 1) {
                tvDealpwd.setText("已设置");
                tvDealpwd.setTextColor(Color.parseColor("#999999"));
            } else {
                tvDealpwd.setText("未设置");
                tvDealpwd.setTextColor(Color.parseColor("#ff3025"));
            }
            SharedPreferencesUtil.getInstance(this).putSP("token", token);

        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

}
